/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.text.*;

/**
 *
 * @author Quang Thao
 */
public class CartMapper {
    DBMapper map;
    
    public CartMapper() throws SQLException
    {
        map = new DBMapper();
    }
    
    public int Add(Bean.CartBean cart) throws Exception
    {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        int kq = 0;
        kq = map.ExecuteNonQuery("Insert Into Cart Values('"
            + cart.getID() + "','" + cart.getAccount_ID() + "'," + cart.getSumCost() + ",'"
            + df.format(cart.getDateCreate()) + "','" + df.format(cart.getDateArrive())
            + "','" + cart.getState() + "','" + cart.getType() + "')");
        if (kq == 1)
        {
           for (int i=0;i<cart.getListBook().size();i++)
           {
               Bean.BookBean book = (Bean.BookBean) cart.getListBook().get(i);
               map.ExecuteNonQuery("Insert Into CartDetail Values('" + cart.getID()
                    + "','" + book.getISBN() + "'," + book.getQuantity() + "," + book.getCost() + ")");
           }    
        }
        return kq;
    }
    
    public String GetNewID() throws Exception
    {
        Date now = new Date();
        DateFormat df = new SimpleDateFormat("ddMM");
        ResultSet rs = map.ExecuteQuery("Select * From Cart Where ID LIKE '" + df.format(now) + "%'");
        int i = 1;
        if (rs != null)
        {
            while (rs.next())
            {
                i++;
            }
        }
        String newID = df.format(now) + String.format("%3d", i);
        return newID;
    }
}
